//
//  TFStatusCell.swift
//  weiboSina
//
//  Created by admin on 16/1/2.
//  Copyright © 2016年 admin. All rights reserved.
//

import UIKit
import SnapKit
// cell 里面的间距
let TFStatusCellMargin: CGFloat = 10
// 头像的宽高
let TFStatusCellIconViewWH: CGFloat = 35

class TFStatusCell: UITableViewCell {
    
    /**
     toolBar的顶部的约束
     */
    var toolBarTopConstraint: Constraint?
    
    
    var statusViewModel:TFStatusViewModel?{
    
        didSet{
        
        //设置数据
            statusOriginalView.statusViewModel = statusViewModel
            statusToolBar.statusViewModel = statusViewModel
            
            
             //更新约束前，先卸载原先的约束
            self.toolBarTopConstraint?.uninstall()
            
            
            //判断是否有转发微博
            if statusViewModel?.status?.retweeted_status != nil{
            
            //有转发微博
                statusRetweetView.hidden = false
                statusRetweetView.statusViewModel = statusViewModel
                //如果有转发微博，底部toolBar 的top 是依靠于转发微博视图的底部来约束的
               statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                
                 self.toolBarTopConstraint = make.top.equalTo(statusRetweetView.snp_bottom).constraint
                
               })
                
            }else{
            
            //没有转发微博
                statusRetweetView.hidden = true
                //如果没有，底部 toolBar 的 top 是依靠于原创微博视图的底部来约束的
            
                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(statusOriginalView.snp_bottom).constraint
                })
                statusRetweetView.statusViewModel = statusViewModel

            }
       }
    
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        /**
        设置选中的样式
    
        */
        selectionStyle = .None
        
        setupUI()
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
  private func setupUI(){
    contentView.backgroundColor = UIColor(white: 0.95, alpha: 1.0)
    //随机颜色
    contentView.addSubview(statusOriginalView)
    contentView.addSubview(statusRetweetView)
    contentView.addSubview(statusToolBar)
    // 添加约束
    statusOriginalView.snp_makeConstraints { (make) -> Void in
        make.leading.equalTo(contentView)
        make.top.equalTo(contentView).offset(10)
        make.trailing.equalTo(contentView)
     }
    statusRetweetView.snp_makeConstraints { (make) -> Void in
        
        make.leading.equalTo(statusOriginalView)
        make.trailing.equalTo(statusOriginalView)
        make.top.equalTo(statusOriginalView.snp_bottom)
        
        
    }
    
    statusToolBar.snp_makeConstraints { (make) -> Void in
        make.leading.equalTo(contentView)
        make.trailing.equalTo(contentView)
       self.toolBarTopConstraint = make.top.equalTo(statusRetweetView.snp_bottom).constraint
        make.height.equalTo(35)
    }
    
    //此处决定cell的高度
    contentView.snp_makeConstraints { (make) -> Void in
        
        make.leading.equalTo(self)
        make.trailing.equalTo(self)
        make.top.equalTo(self)
        make.bottom.equalTo(statusToolBar.snp_bottom)        
    }

 }
    
// MARK: - 懒加载控件
 private lazy var statusOriginalView: TFStatusOriginalView = TFStatusOriginalView()
    //toolBar
    private lazy var statusToolBar: TFStatusToolBar = TFStatusToolBar()
    //转发微博
   private lazy var statusRetweetView: TFStatusRetweetView = TFStatusRetweetView()
}
